<template>
  <transition
    name="custom-classes-transition"
    enter-active-class="animated fadeInDown"
    leave-active-class="animated fadeOutUp"
  >

    <div class="modal-ctrl-body" v-show="addModalObj.isShow">
      <div class="modal-ctrl-main odd-row-ctrl">
        <div class="modal-title">
          <p>新增单兵信息</p>
        </div>
        <div class="modal-ctrl-content">

          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              设备号：
            </div>
            <input type="text" maxlength="16" placeholder="最长16个字符" v-model="AddModalData.EQUIPNUM" class="modal-text f-l">
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              手机：
            </div>
            <input type="text" v-model="AddModalData.TEL" class="modal-text f-l">
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              所属单位：
            </div>
            <input type="text" v-model="stationName" placeholder="请选择所属单位" @click="getStationTree" readonly="readonly" class="modal-text f-l">
            <input v-model="AddModalData.STATION_ID" type="hidden">
            <div class="sectionTree" v-show="sectionTreeIShow">
              <div id="sectionTree" class="ztree sectionTree-box"></div>
            </div>
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l indispensable">
              设备名称：
            </div>
            <input type="text" maxlength="16" placeholder="最长16个字符" v-model="AddModalData.NAME" class="modal-text f-l">
          </div>

          <div class="modal-row">
            <div class="modal-form-key f-l">
              逻辑删除：
            </div>
            <select @change="selectIsDelete" :value="AddModalData.IS_DELETE">
              <option value="0">不删除</option>
              <option value="1">删除</option>
            </select>
          </div>

        </div>
        <div class="btns text-r">
          <button type="button" class="btn btn-default" @click="close">取消</button>
          <button type="button" class="btn btn-info" @click="subBtn">确定</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
    export default {
        name: "AddMonitor",
        props:['addModalObj'],
        data(){
          return{
            AddModalData:{
              EQUIPNUM:'',
              TEL:'',
              STATION_ID:'',
              NAME:'',
              IS_DELETE:'0'
            },
            stationName:'',
            setting : {
              data: {
                simpleData: {
                  enable: true
                },
              }
            },
            sectionTreeIShow:false
          }
        },
        mounted(){
          let _this = this;
          this.setting.callback = {
            onClick:function(event,treeId,treeNode){ //新增警员部门树选择
              //console.log(treeNode);
              _this.AddModalData.STATION_ID = treeNode.id;
              _this.stationName = treeNode.name;
              _this.sectionTreeIShow = !_this.sectionTreeIShow;
            }
          };
          this.$axios.get(this.HOST+'/station/getOrgaInfo')
            .then((data)=>{
              $.fn.zTree.init($("#sectionTree"), this.setting, data.data.data);
              //console.log(data);
            })
            .catch((err)=>{
              //console.log(err);
            })
        },
        methods:{
          //提交
          subBtn(){
            //console.log(this.AddModalData);
            let searchParams = {
              "columns":["*"],
              "conditions":[
                {"field": "EQUIPNUM","symbol":"=","value":this.AddModalData.EQUIPNUM}
              ],
              "dataTypeCode":"policefacility",
              "pageIndex":1,
              "pageSize":20,
              "tableName":"T_RES_DANBING"
            };
            this.$axios.post(this.HOST+"/base/query/page",searchParams)
              .then((data)=>{
                if( data.data.data.list.length > 0 ){
                  this.s_alert("已存在该设备号！");
                }else{
                  if( this.verifyForm.isNull(this.AddModalData.EQUIPNUM) ){
                    this.s_alert("设备号不能为空！");
                    return false;
                  }else if( this.verifyForm.isNull(this.AddModalData.TEL) || this.verifyForm.iphone(this.AddModalData.TEL) ){
                    this.s_alert("手机号为空或者格式不正确！");
                    return false;
                  }else if( this.verifyForm.isNull(this.AddModalData.STATION_ID) ){
                    this.s_alert("所属单位不能为空！");
                    return false;
                  }else if( this.verifyForm.isNull(this.AddModalData.NAME) ){
                    this.s_alert("设备名称不能为空！");
                    return false;
                  }

                  let paramsKey = [];
                  let paramsValue = [];

                  for( let key in this.AddModalData ){
                    paramsKey.push(key);
                    paramsValue.push(this.AddModalData[key]);
                  }

                  let params = {
                    "columns": paramsKey,
                    "dataTypeCode": "policefacility",
                    "datas": paramsValue,
                    "tableName": "T_RES_DANBING"
                  };
                  //console.log(params);
                  this.$axios.post(this.HOST+"/base/save",params)
                    .then((data)=>{
                      //console.log(data);
                      if( data.data.success ){
                        this.s_alert("新增成功！");
                        this.s_logwrite("资源信息管理-单兵管理","新增单兵");
                        this.close()
                      }else{
                        this.s_alert("新增失败！")
                      }
                    })
                    .catch((err)=>{
                      //console.log(err);
                    })
                }
              })
              .catch((err)=>{
              //console.log(err);
            })
          },
          //逻辑删除
          selectIsDelete(ele){
            ////console.log(ele.target.value);
            this.AddModalData.IS_DELETE = ele.target.value
          },
          //展开部门树
          getStationTree(){
            this.sectionTreeIShow = !this.sectionTreeIShow;
          },
          //关闭弹窗
          close(){
            this.clearData(); //关闭弹窗清除数据
            this.$emit("addRadioModalClose");
          },
          //清除数据
          clearData(){
            this.AddModalData={
              EQUIPNUM:'',
              TEL:'',
              STATION_ID:'',
              NAME:'',
              IS_DELETE:'0'
            };
            this.stationName='';
            this.sectionTreeIShow = false;
          }
        }
    }
</script>

<style scoped>
  .sectionTree{
    position: absolute;
    top: 30px;
    right: 0;
    width: 80%;
    z-index: 2;
    background-color: #fff;
    border:1px solid rgba(51,51,51,0.2);
    box-sizing: border-box;
    padding: 5px;
    max-height: 200px;
    overflow-y: auto;
  }
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }
</style>
